package com.revenant.driver.ui.fragment;


import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lljjcoder.style.citylist.Toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.revenant.driver.R;
import com.revenant.driver.adapter.QueRenSijiAdapter;
import com.revenant.driver.adapter.ShipperPublishedItemAdapter;
import com.revenant.driver.base.BaseFragment;
import com.revenant.driver.bean.ShipperBean.QueRenSiBean;
import com.revenant.driver.bean.ShipperBean.ShipperPublishedBean;
import com.revenant.driver.ui.activity.MyGoodsDetailActivity;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import butterknife.BindView;

import static com.revenant.driver.utils.Constants.Goods_Publish_Or_closed;
import static com.revenant.driver.utils.Constants.Que_RenSiJi_Confirm;
import static com.revenant.driver.utils.Constants.Que_RenSiJi_List;
import static com.revenant.driver.utils.Constants.Update_state_goods;

/**
 * A simple {@link Fragment} subclass.
 */
public class ShipperPublishedFragment extends BaseFragment {


    @BindView(R.id.ShipperPublishedrecy)
    RecyclerView       ShipperPublishedrecy;
    @BindView(R.id.ShipperPublishedsmart)
    SmartRefreshLayout ShipperPublishedsmart;
    ShipperPublishedItemAdapter shipperPublishedItemAdapter;
    private int               pagernum = 1;
    private QueRenSijiAdapter mQueRenSijiAdapter;
    private boolean           isDriver = false;

    public ShipperPublishedFragment() {
        // Required empty public constructor
    }


    public static ShipperPublishedFragment newInstance(String param1) {
        ShipperPublishedFragment fragment = new ShipperPublishedFragment();
        Bundle args = new Bundle();
        args.putString(param1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fragment_published;
    }

    @Override
    protected void initView() {
        shipperPublishedItemAdapter = new ShipperPublishedItemAdapter();
        ShipperPublishedrecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        ShipperPublishedrecy.setAdapter(shipperPublishedItemAdapter);
        ShipperPublishedsmart.autoRefresh();


    }

    @Override
    public void initData() {
        //加载更多
        ShipperPublishedsmart.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pagernum++;
                initDataRecyclerview("1");
                ShipperPublishedsmart.finishLoadMore(true);//加载完成
            }
        });
        //刷新
        ShipperPublishedsmart.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                shipperPublishedItemAdapter.setNewData(null);
                pagernum = 1;
                initDataRecyclerview("1");
                ShipperPublishedsmart.finishRefresh(true);//刷新完成
            }
        });

        shipperPublishedItemAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


                Bundle bundle = new Bundle();
                bundle.putString("goods_id", String.valueOf(shipperPublishedItemAdapter.getData().get(position).getId()));
                Intent intent = new Intent();
                intent.putExtras(bundle);
                intent.setClass(getActivity(), MyGoodsDetailActivity.class);
                startActivity(intent);
            }
        });
        shipperPublishedItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.closed_goods_source:
                        setGoodsState(String.valueOf(shipperPublishedItemAdapter.getData().get(position).getId()), "2", position);
                        break;
                    case R.id.conrifm_diver:
                        alertTypeSelect(String.valueOf(shipperPublishedItemAdapter.getData().get(position).getId()));
                        break;
                }
            }
        });


    }

    private void setGoodsState(String goodsid, String isPublish, int posi) {
        OkGo.<String>post(Update_state_goods)

                .params("id", goodsid)
                .params("isPublish", isPublish)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ShipperPublishedBean g = GsonUtil.parseJsonWithGson(response.body(), ShipperPublishedBean.class);
                        if (g.getCode().equals("0")) {
                            Toast.makeText(getActivity(), g .getMsg(), Toast.LENGTH_SHORT).show();
                            shipperPublishedItemAdapter.remove(posi);
                        }
                    }
                });
    }

    private void initDataRecyclerview(String isPublish) {
        OkGo.<String>get(Goods_Publish_Or_closed)

                .params("isPublish", isPublish)
                .params("pageNum", pagernum)
                .params("accountId", SPUtils.getAccounId(getActivity()))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        ShipperPublishedBean shipperPublishedBean = GsonUtil.parseJsonWithGson(response.body(), ShipperPublishedBean.class);
                        int pageNum = shipperPublishedBean.getData().getPageNum();
                        int pages = shipperPublishedBean.getData().getPages();
                        if (shipperPublishedBean.getCode().equals("0")) {
                            if (pageNum == 1) {
                                shipperPublishedItemAdapter.setNewData(shipperPublishedBean.getData().getResult());
                            } else if (pageNum <= pages) {
                                shipperPublishedItemAdapter.addData(shipperPublishedBean.getData().getResult());
                            } else {
                                shipperPublishedItemAdapter.loadMoreEnd();
                            }
//                            if ()
//
//                            if (pagernum == 1) {
//                                shipperPublishedItemAdapter.setNewData(shipperPublishedBean.getData().getResult());
//                            } else if (pagernum < pages) {
//                                shipperPublishedItemAdapter.addData(shipperPublishedBean.getData().getResult());
//                            } else {
//                                shipperPublishedItemAdapter.loadMoreEnd();
//                            }

                        } else {
                            shipperPublishedItemAdapter.loadMoreFail();
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                    }
                });
    }


    private void querensijilist(String id) {
        OkGo.<String>get(Que_RenSiJi_List)

                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        QueRenSiBean queRenSiBean = GsonUtil.parseJsonWithGson(response.body(), QueRenSiBean.class);
                        mQueRenSijiAdapter.setNewData(queRenSiBean.getData());
                        if (queRenSiBean.getData().size() > 0) {
                            isDriver = true;
                        }

                    }
                });

    }

    private void querensiconfirm(String carNo, String id) {
        OkGo.<String>post(Que_RenSiJi_Confirm)

                .params("id", id)
                .params("carNo", carNo)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        QueRenSiBean queRenSiBean = GsonUtil.parseJsonWithGson(response.body(), QueRenSiBean.class);
                        if (queRenSiBean.getCode().equals("0")) {

                        }

                    }
                });
    }

    private void alertTypeSelect(String ids) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getLayoutInflater().inflate(R.layout.querensiji, null);
        RecyclerView type = view.findViewById(R.id.recyclerview);
        TextView cancel = view.findViewById(R.id.cancel);
        mQueRenSijiAdapter = new QueRenSijiAdapter();
        type.setLayoutManager(new LinearLayoutManager(getActivity()));
        type.setAdapter(mQueRenSijiAdapter);
        mQueRenSijiAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.click:
                        querensiconfirm(
                                mQueRenSijiAdapter.getData().get(position).getCarNo(),
                                String.valueOf(mQueRenSijiAdapter.getData().get(position).getGoodsId())

                        );
                        break;
                }
            }
        });
        builder.setView(view);
        querensijilist(ids);
        if (isDriver) {
            final AlertDialog alertDialog = builder.show();

            final Window window = alertDialog.getWindow();
            window.setBackgroundDrawable(new ColorDrawable(0));
            cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();

                }
            });
        } else {
            ToastUtils.showShortToast(getActivity(),"请稍后");
        }

    }

}
